package com.gaosai.staff.commlistadapter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.answer_staff.R;
import com.example.answer_staff.RegisterActivity;
import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.view.CustomFlowLayout;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ListAndActivityListAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<String>list;
	private String types;
	private LayoutInflater mInflater ;
	private DisplayImageOptions options; 
	private static final int NEWS = 0 ;
	private static final int ACTIIVTYS = 1 ;
	private String subject_activity ;
	private String briefContent_activity ;
	private int start_time_activity ;
	private int end_time_activity ;
	private String venue_activity ;
	private String pic_news ;
	private String title_news ;
	private String briefContent_news ;
	private int addTime_news ;
	private JSONArray tags ;
	private JSONArray activityTags ;
	private String pic_banner;
	public ListAndActivityListAdapter(Context context,ArrayList<String>list) {
		this.context=context;
		this.list=list;	
		mInflater = LayoutInflater.from(context) ;
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_launcher)
		.showImageForEmptyUri(R.drawable.ic_launcher)
		.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
		.cacheOnDisc(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();

	}

	@Override
	public int getViewTypeCount() {
		return 2 ;
	}

	@Override
	public int getItemViewType(int position) {
		final String str = list.get(position) ;
		JSONObject jsonObject;
		String type = null ;
		try {
			jsonObject = new JSONObject(str);
			type = jsonObject.getString("types");
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return type.equals("new")?NEWS:ACTIIVTYS ;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	@Override
	public long getItemId(int postion) {
		return postion;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final String str = list.get(position) ;

		JsonParse(str);
		int type_item=getItemViewType(position);
		Log.e("type", type_item+"") ;		
		ViewHolder holder = null;

		if (convertView == null) {
			holder = new ViewHolder();

			switch (type_item) { //根据不同的类型为不同的item初始化组件
			case NEWS: //新闻
				convertView = mInflater.inflate(R.layout.newsroom_item, parent,false);
				holder.pic_news  = (ImageView) convertView.findViewById(R.id.imageView) ;
				holder.title_news  = (TextView) convertView.findViewById(R.id.textView1);
				holder.briefContent_news  = (TextView) convertView.findViewById(R.id.textView2);
				holder.addTime_news  = (TextView) convertView.findViewById(R.id.textView3);
				holder.newsflowLayout = (CustomFlowLayout) convertView.findViewById(R.id.news_flowlayout) ;
				break;
			case ACTIIVTYS://活动
				convertView = mInflater.inflate(R.layout.activity_item, parent,false);
				holder.pic_activity  = (ImageView) convertView.findViewById(R.id.imageView_activity) ;
				holder.subject_activity = (TextView) convertView.findViewById(R.id.title) ;
				holder.briefContent_activity = (TextView) convertView.findViewById(R.id.content_bf) ;
				holder.start_time_activity = (TextView) convertView.findViewById(R.id.time) ;
				holder.end_time_activity = (TextView) convertView.findViewById(R.id.date) ;
				holder.venue_activity = (TextView) convertView.findViewById(R.id.venue) ;
				holder.relay=(RelativeLayout)convertView.findViewById(R.id.regis_bottom_listeners_activity);
				holder.activityFlowLayout = (CustomFlowLayout) convertView.findViewById(R.id.activity_flowlayout) ;
				break ;
			default:
				break;
			}
			convertView.setTag(holder);

		}else{
			holder = (ViewHolder) convertView.getTag() ;
		}

		switch (type_item) { //根据不同的类型，为不同的item设置数据
		case NEWS: //新闻

			/**
			 * 显示图片
			 * pic——news是一个服务器取出的图片的路径：
			 * holder.pic_news图片重用的控件；
			 * 
			 */
			ImageLoader.getInstance().displayImage(pic_news, holder.pic_news,options) ;
			holder.title_news.setText(title_news);
			holder.briefContent_news.setText(briefContent_news);
			holder.addTime_news.setText(CommonUtils.getDateToStringt(addTime_news));	
			holder.newsflowLayout.removeAllViews() ; //先把布局流中的标签清空，再添加，否则出出现 滚动的时候重复添加
			for (int i = 0; i < tags.length(); i++) {
				TextView tv = (TextView) mInflater.inflate(R.layout.tv,holder.newsflowLayout, false);
				String tagName = tags.optJSONObject(i).optString("name") ;
				//取得缩写名字
				String abbreName = CommonUtils.getAbbreviation(tagName) ;
				tv.setText(abbreName) ;
				CommonUtils.setColor(abbreName,tv,context) ;
				holder.newsflowLayout.addView(tv) ;
			}
			break ;

		case ACTIIVTYS://活动
			ImageLoader.getInstance().displayImage(pic_banner, holder.pic_activity,options) ;
			holder.subject_activity.setText(subject_activity);
			holder.briefContent_activity.setText(briefContent_activity);
			holder.start_time_activity.setText(CommonUtils.getDateToStringt(start_time_activity));
			holder.end_time_activity.setText(CommonUtils.getDateToStringt(end_time_activity));
			holder.venue_activity.setText(venue_activity+"");
			holder.relay.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Intent intent=new Intent(context,RegisterActivity.class);
					intent.putExtra("application_list", list.get(position));
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(intent);
				}
			});

			holder.activityFlowLayout.removeAllViews() ;//先把布局流中的标签清空，再添加，否则出出现 滚动的时候重复添加
			for (int i = 0; i < activityTags.length(); i++) {
				TextView tv = (TextView) mInflater.inflate(R.layout.tv,holder.activityFlowLayout, false);
				String tagName = activityTags.optJSONObject(i).optString("name") ;
				//取得缩写名字
				String abbreName = CommonUtils.getAbbreviation(tagName) ;
				tv.setText(abbreName) ;
				CommonUtils.setColor(abbreName,tv,context) ;
				holder.activityFlowLayout.addView(tv) ;
			}
			break ;
		}
		return convertView;
	}
	private void JsonParse(String json) {
		JSONObject jsonObject=null;
		try {
			jsonObject = new JSONObject(json);
			types = jsonObject.getString("types");
			if(types.equals("activity")){
				//   字段是：subject,brief_content,start_time,end_time,venue 
				pic_banner=jsonObject.getString("banner");
				subject_activity=jsonObject.getString("subject");
				briefContent_activity=jsonObject.getString("briefContent");
				start_time_activity=jsonObject.getInt("startTime");
				end_time_activity=jsonObject.getInt("endTime");
				venue_activity=jsonObject.getString("venue");
				activityTags = jsonObject.optJSONArray("tags") ;
			}else{
				//   字段：	pic  title  brief_content add_time 
				pic_news=jsonObject.getString("pic");
				title_news=jsonObject.getString("title");
				briefContent_news=jsonObject.getString("briefContent");
				addTime_news=jsonObject.getInt("addTime");
				tags = jsonObject.optJSONArray("TTagses") ;
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private class ViewHolder{
		private TextView subject_activity ;
		private TextView briefContent_activity ;
		private TextView start_time_activity ;
		private TextView end_time_activity ;
		private TextView venue_activity ;
		private ImageView pic_news ;
		private TextView title_news ;
		private TextView briefContent_news ;
		private TextView addTime_news ;
		private RelativeLayout relay;
		private ImageView pic_activity ; 
		/**新闻的流式布局**/
		private CustomFlowLayout newsflowLayout ;
		/**活动的流式布局**/
		private CustomFlowLayout activityFlowLayout ;
	}

}
